package com.android.systemui.ios.wall;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;

import com.android.systemui.ios.R;

public class IpPagerIndicator extends View {

    private Paint mSolid;
    private Paint mHollow;
    private Paint mShadow;

    private final int mCircleRadius;
    private final int mGap;
    private int mCount = 2;
    private int mCurrent = 0;
    private int mGravity = Gravity.CENTER;

    public IpPagerIndicator(Context context) {
        this(context, null, 0);
    }

    public IpPagerIndicator(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IpPagerIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        Resources res = getResources();
        mCircleRadius = res.getDimensionPixelSize(R.dimen.pager_indicator_circle_radius);
        mGap = res.getDimensionPixelSize(R.dimen.pager_indicator_circle_gap);
    }

    public void setCurrent(int position) {
        mCurrent = position;
        invalidate();
    }

    public void setCount(int count) {
        mCount = count;
        if (mCurrent > mCount) {
            mCurrent = 0;
        }
        invalidate();
    }

    public int getCurrent() {
        return mCurrent;
    }

    public void setGravity(int gravity) {
        mGravity = gravity;
        invalidate();
    }

    public void clean() {
        mCount = 0;
        mCurrent = 0;
        invalidate();
    }

    public void add(int count) {
        mCount += count;
        invalidate();
    }

    public void addOne() {
        mCount++;
        invalidate();
    }

    public void removeOne() {
        if (mCount > 0) {
            mCount--;
            invalidate();
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mSolid = new Paint();
        mSolid.setColor(Color.WHITE);
        mSolid.setAntiAlias(true);
        mSolid.setStyle(Paint.Style.FILL);

        mHollow = new Paint(mSolid);
        mHollow.setStyle(Paint.Style.FILL);
        mHollow.setColor(0x56FFFFFF);

        mShadow = new Paint(mSolid);
        mShadow.setStyle(Paint.Style.STROKE);
        mShadow.setStrokeWidth(1.0f);
        mShadow.setColor(0x13000000);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mCount > 1 && mCurrent >= 0 && mCurrent < mCount) {
            int cy = getPaddingTop() + getHeight() / 2 - 2;
            int cx = 0;
            switch (mGravity) {
                case Gravity.LEFT:
                    cx = mGap + mCircleRadius;
                    break;
                case Gravity.CENTER:
                    cx = (getWidth() - mCount * mCircleRadius * 2 - mGap * (mCount - 1)) / 2;
                    break;
                default:
                    cx = (getWidth() - mCount * mCircleRadius * 2 - (mCount - 1) * mGap) / 2 + mCircleRadius;
                    break;
            }
            for (int i = 0; i < mCount; i++) {
                canvas.drawCircle(cx, cy, mCircleRadius, mCurrent == i ? mSolid : mHollow);
                canvas.drawCircle(cx, cy, mCircleRadius + 1, mShadow);
                cx += mCircleRadius * 2 + mGap;
            }
        }
    }
}
